//
//  CHSuperviseCell.m
//  CarFile
//
//  Created by 陈浩 on 2017/4/1.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHSuperviseCell.h"
@interface CHSuperviseCell()

@property (nonatomic ,strong) UILabel *titleLabel;    // 标题
@property (nonatomic ,strong) UILabel *leftTitleLabel;   // 左边标题
@property (nonatomic ,strong) UILabel *leftPercentLabel;   // 左边百分比
@property (nonatomic ,strong) UILabel *rightTitleLabel;   // 右边标题
@property (nonatomic ,strong) UILabel *rightPercentLabel;   // 右边百分比
@property (nonatomic ,strong) UIImageView *iconImgView;  //  图标
@property (nonatomic ,strong) UIView *line;

@end
@implementation CHSuperviseCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
+(instancetype)cellForTableView:(UITableView *)tableView{
    
    static NSString *ID = @"superviseCell";
    CHSuperviseCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[CHSuperviseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
    
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self buildUI];
        
    }
    return self;
}



-(void)buildUI{

    self.iconImgView = [[UIImageView alloc] init];
    [self addSubview:self.iconImgView];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    self.titleLabel.text = @"联网对接情况";
    self.titleLabel.textColor = [UIColor colorWithHexString:@"333333" alpha:1.0f];
    [self addSubview:self.titleLabel];
    
    
    self.leftPercentLabel = [[UILabel alloc] init];
    self.leftPercentLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:24];
    self.leftPercentLabel.textAlignment = NSTextAlignmentCenter;
    self.leftPercentLabel.textColor = KRGB(117, 202, 48);
    [self addSubview:self.leftPercentLabel];
    
    self.rightPercentLabel = [[UILabel alloc] init];
    self.rightPercentLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:24];
    self.rightPercentLabel.textAlignment = NSTextAlignmentCenter;
    self.rightPercentLabel.textColor = KRGB(251, 91, 111);
    [self addSubview:self.rightPercentLabel];
    
    
    self.leftTitleLabel = [[UILabel alloc] init];
    self.leftTitleLabel.text = @"已对接";
    self.leftTitleLabel.font = [UIFont systemFontOfSize:12];
    self.leftTitleLabel.textAlignment = NSTextAlignmentCenter;
    self.leftTitleLabel.textColor = [UIColor colorWithHexString:@"999999" alpha:1.0f];
    [self addSubview:self.leftTitleLabel];
    
    
    self.rightTitleLabel = [[UILabel alloc] init];
    self.rightTitleLabel.text = @"未对接";
    self.rightTitleLabel.textAlignment = NSTextAlignmentCenter;
    self.rightTitleLabel.font = [UIFont systemFontOfSize:12];
    self.rightTitleLabel.textColor = [UIColor colorWithHexString:@"999999" alpha:1.0f];
    [self addSubview:self.rightTitleLabel];
    
    self.line = [[UIView alloc] init];
    self.line.backgroundColor = [UIColor colorWithHexString:@"999999" alpha:0.3f];
    [self addSubview:self.line];
    
    UIButton *leftBtn = [[UIButton alloc] init];
    leftBtn.backgroundColor = [UIColor clearColor];
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftBtn];
    self.leftBtn = leftBtn;

    UIButton *rightBtn = [[UIButton alloc] init];
    rightBtn.backgroundColor = [UIColor clearColor];
    [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightBtn];
    self.rightBtn = rightBtn;
    
}



-(void)leftBtnClick{
    
    if (self.leftBlock) {
        self.leftBlock();
    }
}


-(void)rightBtnClick{
    if (self.rightBlock) {
        self.rightBlock();
    }
    
}



-(void)setModel:(CHSuperviseModel *)model{
    _model = model;
    self.iconImgView.image = [UIImage imageNamed:model.iconName];
    self.titleLabel.text = model.title;
    if (model.leftPercent == 0.00) {
        self.leftPercentLabel.text = @"0.00%";
    }else{
        self.leftPercentLabel.text = [NSString stringWithFormat:@"%.2f%%",model.leftPercent/(model.leftPercent + model.rightPercent) *100];
    }
    if (model.rightPercent == 0.00) {
        self.rightPercentLabel.text = @"0.00%";
    }else{
        self.rightPercentLabel.text = [NSString stringWithFormat:@"%.2f%%",model.rightPercent/(model.leftPercent + model.rightPercent) *100];
    }
    self.leftTitleLabel.text = model.leftTitle;
    self.rightTitleLabel.text = model.rightTitle;
    
  
}

-(void)layoutSubviews{
    [super layoutSubviews];

    self.iconImgView.frame = CGRectMake(15, 15, 20, 20);
    self.titleLabel.frame = CGRectMake(45, 10, UIScreenW - 55, 30);
    self.leftPercentLabel.frame = CGRectMake(0, 50, UIScreenW /2, 40);
    self.rightPercentLabel.frame = CGRectMake(UIScreenW /2, 50, UIScreenW /2, 40);
    self.line.frame = CGRectMake(UIScreenW /2, 60, 1, 50);
    self.leftTitleLabel.frame = CGRectMake(0, CGRectGetMaxY(self.leftPercentLabel.frame), UIScreenW /2, 20);
    self.rightTitleLabel.frame = CGRectMake(UIScreenW /2, CGRectGetMaxY(self.rightPercentLabel.frame), UIScreenW /2, 20);
    
    self.leftBtn.frame = CGRectMake(0, 0, UIScreenW/2, self.h);
    self.rightBtn.frame = CGRectMake(UIScreenW/2, 0, UIScreenW/2, self.h);
    
    
    
}



@end
